﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for clsDIEMDANH
/// </summary>
public class clsDIEMDANH
{
	public clsDIEMDANH()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    #region Properties
    public static string GetLastId()
    {
        string Id = "";
        DataTable dt = TableLastRow();
        if (dt.Rows.Count > 0)
        {
            Id = dt.Rows[0]["F_MADD"].ToString();
        }
        return Id;
    }
    #endregion

    #region Lấy danh sách học viên theo lớp và tháng điểm danh
    public static DataTable GetListStudenByClass(Guid F_MADD, string F_MALOP)
    {
        DataTable dt = new DataTable();
        DatabaseConnect connect = new DatabaseConnect();
        SqlParameter[] para = new SqlParameter[2];
        para[0] = new SqlParameter("@F_MADD", F_MADD);
        para[1] = new SqlParameter("@F_MALOP", F_MALOP);
        try {
            dt = connect.GetSelectCommand("DIEMDANH_CHITIET_GetListStudentByClass", para);
        }
        catch { }        
        return dt;
    }
    #endregion

    #region GetList
    public static DataTable GetList()
    {
        DataTable dt = new DataTable();
        DatabaseConnect connect = new DatabaseConnect();
        try
        {
            dt = connect.GetSelectCommand("DIEMDANH_GetList", null);
        }
        catch { }
        return dt;
    }
    #endregion

    #region NewId
    private static Guid NewId()
    {
        Guid id = Guid.NewGuid();
        DataTable dt;
        bool temp = true;
        while (temp == true)
        {
            dt = GetById(id);
            if (dt.Rows.Count > 0)
            {
                id = Guid.NewGuid();
                temp = true;
            }
            else
            {
                temp = false;
            }
        }
        return id;
    }
    #endregion

    #region GetById
    public static DataTable GetById(Guid Id)
    {
        DataTable dt = new DataTable();
        DatabaseConnect connect = new DatabaseConnect();
        SqlParameter[] para = new SqlParameter[1];
        para[0] = new SqlParameter("@Id", Id);
        try
        {
            dt = connect.GetSelectCommand("DIEMDANH_GetById", para);
        }
        catch { }
        return dt;

    }
    #endregion

    #region Insert
    public static string Insert(int thang, int nam)
    {
        if (IsSame(thang, nam) == true) // Kiểm tra xem tháng và năm có tồn tại trong dữ liệu chưa
        {
            return "SAME";
        }
        DatabaseConnect connect = new DatabaseConnect();
        Guid Id = NewId();
        SqlParameter[] para = new SqlParameter[3];
        para[0] = new SqlParameter("@F_MADD", Id);
        para[1] = new SqlParameter("@F_THANG", thang);
        para[2] = new SqlParameter("@F_NAM", nam);
        try
        {
            connect.ExecuteQuery("DIEMDANH_Insert", para);
            return "OK";
        }
        catch
        {
            return "ERR";
        }
    }
    #endregion

    #region GetByMonthAndYear
    public static DataTable GetBYMonthAndYear(int F_THANG, int F_NAM)
    {
        DataTable dt = new DataTable();
        DatabaseConnect connect = new DatabaseConnect();
        SqlParameter[] para = new SqlParameter[2];
        para[0] = new SqlParameter("@F_THANG", F_THANG);
        para[1] = new SqlParameter("@F_NAM", F_NAM);
        try
        {
            dt = connect.GetSelectCommand("DIEMDANH_GetByMonthAndYear", para);
        }
        catch { }
        return dt;

    }
    #endregion

    #region IsSame
    public static bool IsSame(int month, int year)
    {
        DataTable dt = GetBYMonthAndYear(month, year);
        if (dt.Rows.Count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    #endregion

    #region Update
    public static string Update(string F_MAHV, Guid F_MADD, string F_DAY, string VALUE)
    {
        DatabaseConnect connect = new DatabaseConnect();
        SqlParameter[] para = new SqlParameter[4];
        para[0] = new SqlParameter("@F_MAHV", F_MAHV);
        para[1] = new SqlParameter("@F_MADD", F_MADD);
        para[2] = new SqlParameter("@F_DAY", F_DAY);
        para[3] = new SqlParameter("@VALUE", VALUE);
        try
        {
            connect.ExecuteQuery("DIEMDANH_CHITIET_Update", para);
            return "OK";
        }
        catch
        {
            return "ERR";
        }
    }
    #endregion

    #region Update By Class
    public static string UpdateByClass(string F_MALOP, Guid F_MADD, int F_DAY, string F_VALUE)
    {
        DatabaseConnect connect = new DatabaseConnect();
        SqlParameter[] para = new SqlParameter[4];
        para[0] = new SqlParameter("@F_MALOP", F_MALOP);
        para[1] = new SqlParameter("@F_MADD", F_MADD);
        para[2] = new SqlParameter("@F_DAY", F_DAY);
        para[3] = new SqlParameter("@F_VALUE", F_VALUE);
        try
        {
            connect.ExecuteQuery("DIEMDANH_CHITIET_UpdateByClass", para);
            return "OK";
        }
        catch
        {
            return "ERR";
        }
    }
    #endregion

    #region Table Get Last Row
    static DataTable TableLastRow()
    {
        DataTable dt = new DataTable();
        DatabaseConnect connect = new DatabaseConnect();
        try
        {
            dt = connect.GetSelectCommand("DIEMDANH_GetLastRow", null);
        }
        catch { }
        return dt;
    }
    #endregion
}